//:  v.book.read.tip.observer.MyFlower.java
package v.book.read.tip.observer;

import java.util.Observable;

/**
 *
 * @author 小败
 */
public class MyFlower {
    
    private boolean isOpen;
    private OpenNotifier oNotifier = new OpenNotifier();
    private CloseNotifier cNotifier = new CloseNotifier();
    
    public MyFlower() {
        this.isOpen = false;
    }
    
    private void open() {
        this.isOpen = true;
        oNotifier.notifyObservers();
       cNotifier.open();
        
    }
    
    private void close() {
        this.isOpen = false;
        cNotifier.notifyObservers();
        oNotifier.close();
    }
    
    public OpenNotifier opening() {
        return oNotifier;
    }
    
    public CloseNotifier closeing() {
        return cNotifier;
    }
    
    private class OpenNotifier extends Observable {
        private boolean alreadyOpen = false;

        public void notifyObservers() {
            if (isOpen && !alreadyOpen) {
                setChanged();
                super.notifyObservers();
                alreadyOpen = true;
            }
        }

        public void close() {
            alreadyOpen = false;
        }
    }

    private class CloseNotifier extends Observable {
        private boolean alreadyClosed = false;

        public void notifyObservers() {
            if (!isOpen && !alreadyClosed) {
                setChanged();
                super.notifyObservers();
                alreadyClosed = true;
            }
        }

        public void open() {
            alreadyClosed = false;
        }
    }

}
